﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XQ.Framework.core
{
    public class DateTimeHelper
    {
        public static int getYear()
        {
            return DateTime.Now.Year;
        }
        public static int getMonth()
        {
            return DateTime.Now.Month;
        }
        public static int getDay()
        {
            return DateTime.Now.Day;
        }
        public static int getHour()
        {
            return DateTime.Now.Hour;
        }
        public static int getMinute()
        {
            return DateTime.Now.Minute;
        }
        public static int getSecond()
        {
            return DateTime.Now.Second;
        }
        public static string getTime()
        {
            DateTime dt = DateTime.Now;
            return string.Format("{0}:{1}:{2}", dt.Hour, dt.Minute, dt.Second);
        }
        public static string getDate()
        {
            DateTime dt = DateTime.Now;
            return string.Format("{0}-{1}-{2}", dt.Year, dt.Month, dt.Day);
        }
        public static string getChineaseDate()
        {
            DateTime dt = DateTime.Now;
            return string.Format("{0}年{1}月{2}日", dt.Year, dt.Month, dt.Day);
        }
        public static string getDateTime()
        {
            DateTime dt = DateTime.Now;
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}",
                dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
        }
        public static int getDayOfWeek()
        {
            return (int)DateTime.Now.DayOfWeek;
        }
        public static int getDayOfYear()
        {
            return (int)DateTime.Now.DayOfYear;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string getWeek()
        {
            string week = string.Empty;
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Sunday: week = "星期日"; break;
                case DayOfWeek.Monday: week = "星期一"; break;
                case DayOfWeek.Tuesday: week = "星期二"; break;
                case DayOfWeek.Wednesday: week = "星期三"; break;
                case DayOfWeek.Thursday: week = "星期四"; break;
                case DayOfWeek.Friday: week = "星期五"; break;
                case DayOfWeek.Saturday: week = "星期六"; break;
                default: week = "unknow Week"; break;
            }
            return week;
        }
        public static string getMysqlFormatDateTime()
        {
            return getDateTime();
        }
        public static string getFriendlyDateTime(string time)
        {
            if (!ValidateHelper.IsPlumpString(time))
            {
                return "未知时间";
            }
            DateTime dft = DateTime.Now;
            DateTime dt = ConvertHelper.getDateTime(time, dft);
            if (dft == dt)
            {
                return time;
            }
            return getFriendlyDateTime(dt);
        }
        public static string getFriendlyDateTime(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.TotalDays > 60)
            {
                return dt.ToShortDateString();
            }
            else if (span.TotalDays > 30)
            {
                return "1个月前";
            }
            else if (span.TotalDays > 14)
            {
                return "2周前";
            }
            else if (span.TotalDays > 7)
            {
                return "1周前";
            }
            else if (span.TotalDays > 1)
            {
                return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
            }
            else if (span.TotalHours > 1)
            {
                return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
            }
            else if (span.TotalMinutes > 1)
            {
                return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
            }
            else if (span.TotalSeconds >= 1)
            {
                return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
            }
            else
            {
                return "1秒前";
            }
        }
    }
}
